package org.haredot.controller;

import org.haredot.annotation.LogModule;
import org.haredot.entity.Func;
import org.haredot.service.FuncService;
import org.haredot.vo.PageVo;
import org.haredot.vo.ResultVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@LogModule("功能管理")
@RestController
@RequestMapping("/func")
public class FuncController {

    @Resource
    private FuncService funcService ;

    /**
     * 添加功能
     * @param func
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('func:view:add')")
    public ResultVo<Func> addFunc(@RequestBody Func func) {
        return funcService.saveFunc(func) ;
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('func:view')")
    public ResultVo<PageVo<Func>> addFunc(@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
                                          @RequestParam(value = "limits", required = false, defaultValue = "10")Integer limits,
                                          @RequestParam Map<String, String> params) {

        PageVo<Func> pageVo = new PageVo<>(page, limits);
        // 设置查询参数
        pageVo.setParams(params);

        return funcService.queryFuncsList(pageVo) ;
    }

    @PutMapping("/{funcId}")
    @PreAuthorize("hasAuthority('func:view:modify')")
    public ResultVo<Func> modifyMenu(@PathVariable("funcId") Long funcId,
                                     @RequestBody Func func) {
        func.setId(funcId);
        return funcService.modifyFunc(func);
    }

    @DeleteMapping("/del{funcId}")
    @PreAuthorize("hasAuthority('func:view:del')")
    public ResultVo<?> removeFunc(@PathVariable Long funcId) {

        return funcService.removeFunc(funcId);
    }
}
